﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using RM.Common;
using RM.DBUtility;
namespace RM.DAL
{
    public partial class RMRoleUser
    {
        public RMRoleUser()
        { }
        /// <summary>
        /// 判断数据是否存在。
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,roleid,userid from x_roleuser");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters ={
                                          new SqlParameter("@id",SqlDbType.Int)
                                      };
            parameters[0].Value = id;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.RMRoleUser model)
        {
            using (SqlConnection Conn = new SqlConnection(PubConstant.ConnectionString))
            {
                Conn.Open();
                using (SqlTransaction trans = Conn.BeginTransaction())
                {
                    try
                    {
                        StringBuilder strSql = new StringBuilder();
                        strSql.Append("insert into  X_RoleUser (");
                        strSql.Append("roleid,");
                        strSql.Append("userid)");
                        strSql.Append(" values(");
                        strSql.Append("@roleid,");
                        strSql.Append("@userid)");
                        strSql.Append(";select @@IDENTITY");
                        SqlParameter[] parameters ={
                                                      new SqlParameter("@roleid",SqlDbType.Int),
                                                      new SqlParameter("@userid",SqlDbType.Int)};
                        object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
                        model.Id = Convert.ToInt32(obj);
                        trans.Commit();
                    }
                    catch
                    {
                        trans.Rollback();
                        return 0;
                    }
                }
            }
            return model.Id;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.RMRoleUser model)
        {
            using (SqlConnection Conn = new SqlConnection(PubConstant.ConnectionString))
            {
                Conn.Open();
                using (SqlTransaction trans = Conn.BeginTransaction())
                {
                    try
                    {
                        StringBuilder strSql = new StringBuilder();
                        strSql.Append("update X_RoleUser set ");
                        strSql.Append("roleid=@roleid,");
                        strSql.Append("userid=@userid");
                        strSql.Append(" where id=@id");
                        SqlParameter[] parameters ={
                                                      new SqlParameter("@roleid",SqlDbType.Int),
                                                      new SqlParameter("@userid",SqlDbType.Int),
                                                      new SqlParameter("@id",SqlDbType.Int)};
                        parameters[0].Value = model.RoleID;
                        parameters[1].Value = model.UserID;
                        parameters[2].Value = model.Id;
                        DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
                        trans.Commit();
                    }
                    catch
                    {
                        trans.Rollback();
                        return false;
                    }
                }
            }
            return true;
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from X_RoleUser");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters ={
                                          new SqlParameter("@id",SqlDbType.Int)};
            parameters[0].Value = id;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.RMRoleUser GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,roleid,userid from X_RoleUser");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters ={
                                          new SqlParameter("@id",SqlDbType.Int)};
            parameters[0].Value = id;
            Model.RMRoleUser model = new Model.RMRoleUser();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["roleid"].ToString() != "")
                {
                    model.RoleID = int.Parse(ds.Tables[0].Rows[0]["roleid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["userid"].ToString() != "")
                {
                    model.UserID = int.Parse(ds.Tables[0].Rows[0]["userid"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获得查询数据分页
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string fieldOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from X_RoleUser");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), fieldOrder));
        }
        
    }

}
